package v.sched.quite;

import v.sched.quite.intent.QuiteIntents;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.KeyEvent;

import com.admob.android.ads.AdManager;

public class QuiteNight extends PreferenceActivity {
	

	public static final String LOG_TAG = QuiteNight.class.getSimpleName();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AdManager.setPublisherId("a14c5e6a8c9f623");
		AdManager.setTestDevices( new String[] {
			      AdManager.TEST_EMULATOR
			      } );

		setContentView(R.layout.quite_night);
		addPreferencesFromResource(R.xml.settings);
		
		Preference p = findPreference("startNightTime");
		p.setDefaultValue("22:30");
		
		p = findPreference("endNightTime");
		p.setDefaultValue("7:00");
		
		p = findPreference("author");
		p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			@Override
			public boolean onPreferenceClick(Preference preference) {
				Intent i = new Intent(Intent.ACTION_SEND);  
				//i.setType("text/plain"); //use this line for testing in the emulator  
				i.setType("message/rfc822") ; // use from live device
				i.putExtra(Intent.EXTRA_EMAIL, new String[]{"vincenzo.gammieri@gmail.com"});  
				i.putExtra(Intent.EXTRA_SUBJECT,"QuiteNight");  
				i.putExtra(Intent.EXTRA_TEXT,"...");  
				startActivity(Intent.createChooser(i,null));
				return true;
				
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			setTimers();    
	    }

	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		
	}

	private void setTimers() {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		boolean monitoringEnabled = sp.getBoolean("monitoringEnabled", false);
		if (monitoringEnabled)
			QuiteReceiver.setNotificationAlarms(getApplicationContext());
		else{
			QuiteReceiver.deleteNotificationAlarms(getApplicationContext(),QuiteIntents.START_NIGHT_INTENT);
			QuiteReceiver.deleteNotificationAlarms(getApplicationContext(),QuiteIntents.END_NIGHT_INTENT);
		}
	}

//	private void setNotificationAlarms(Context context) {
//		
//		SharedPreferences sp = PreferenceManager
//				.getDefaultSharedPreferences(context);
//		
//		String startNight = sp.getString("startNightTime","");
//		String endNight = sp.getString("endNightTime","");
//		
//		
//		String s ="";
//		String slabel = getResources().getString(R.string.toast_start_night);
//		String elabel = getResources().getString(R.string.toast_end_night);
//		
//		s = slabel;
//		s += ":";
//		s+=startNight;
//		s+="\n";
//		s+=elabel;
//		s+=":";
//		s+=endNight;
//		Toast.makeText(getApplicationContext(), s, 2000).show();
//		
//		QuiteReceiver.scheduleAction(context, QuiteIntents.START_NIGHT_INTENT, startNight, true);
//		QuiteReceiver.scheduleAction(context, QuiteIntents.END_NIGHT_INTENT, endNight, false);
//		
//		
//	}

	
}